
package com.wzy.facetextview.Utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.Log;

import com.wzy.facetextview.widget.FaceManager;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * URL点击处理
 *
 * @author wangzhenyu
 */
public class CommonURLClickHelper {
    private static final String TAG = "wang.URLClickHelper";
    private Context mContext;

    public CommonURLClickHelper() {
        super();
    }

    public SpannableStringBuilder formatURLMessage(Context context, String text) {
        mContext = context;
        try {
            // 过滤html超链接标签
            String textTmp = text.replaceAll(ConstantsUtil.REGEXP_LINK_BEGIN, "");
            textTmp = textTmp.replaceAll(ConstantsUtil.REGEXP_LINK_END, "");
            // 过滤换行符
            textTmp = textTmp.replaceAll(ConstantsUtil.REGEXP_NEWLINE, "");
            // 过滤转义的&符号
            textTmp = textTmp.replaceAll(ConstantsUtil.REGEXP_AND, "&");
            // 匹配表情
            Pattern facePattern = Pattern.compile(ConstantsUtil.REGEXP_IMG);
            Matcher faceMatcher = facePattern.matcher(textTmp);
            ArrayList<String> faceStrs = new ArrayList<String>();
            // faceBgin、faceEnd保存表情的起止位置
            ArrayList<Integer> faceBegin = new ArrayList<Integer>();
            ArrayList<String> faceholder = new ArrayList<String>();
            while (faceMatcher.find()) {
                String tmp = faceMatcher.group();
                faceStrs.add(tmp);
                faceBegin.add(textTmp.indexOf(tmp));
                String replace = ConstantsUtil.FACE_HOLDER_BEGIN + faceStrs.indexOf(tmp)
                        + ConstantsUtil.FACE_HOLDER_END;
                textTmp = textTmp.replaceFirst(tmp, replace);
                faceholder.add(replace);
                faceMatcher = facePattern.matcher(textTmp);
            }
            Log.d(TAG, "textTmp" + textTmp);
            // 匹配url
            Pattern urlPattern = Pattern.compile(ConstantsUtil.REGEXP_URL, Pattern.CASE_INSENSITIVE);
            Matcher urlMatcher = urlPattern.matcher(textTmp);
            ArrayList<String> urlStrs = new ArrayList<String>(); // 多个url
            ArrayList<Integer> urlBegin = new ArrayList<Integer>();
            int searchStart = 0;
            while (urlMatcher.find()) {
                String tmp = urlMatcher.group();
                urlStrs.add(tmp);
                int urlBeginTmp = textTmp.indexOf(tmp, searchStart);
                urlBegin.add(urlBeginTmp);
                searchStart = urlBeginTmp + tmp.length() + 1;
            }

            SpannableStringBuilder spStr = new SpannableStringBuilder(textTmp);
            // 添加url样式
            if (urlStrs.size() != 0) {
                for (int i = 0; i < urlStrs.size(); i++) {
                    String urlTmp = urlStrs.get(i);
                    TextLinkClickSpan mTextClickSpan = new TextLinkClickSpan(new URLClickListener(mContext, urlTmp));
                    Log.d(TAG, "urlTmp:" + urlTmp);
                    int begin = urlBegin.get(i);
                    int end = begin + urlTmp.length();
                    spStr.setSpan(mTextClickSpan, begin, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    spStr = new SpannableStringBuilder(spStr);
                }
            }
            Log.d(TAG, "SpannableStringBuilder:" + spStr);
            // 显示表情
            int replacedLength = 0;
            if (faceStrs.size() != 0) {
                for (int i = 0; i < faceStrs.size(); i++) {
                    SpannableStringBuilder face = (SpannableStringBuilder) Html.fromHtml(faceStrs.get(i), mImageGetter,
                            null);
                    int begin = faceBegin.get(i).intValue() - replacedLength;
                    int holderLength = faceholder.get(i).length(); // 表情占位符[vivoface0]长度
                    replacedLength = replacedLength + (holderLength - 1);
                    spStr.replace(begin, begin + holderLength, face);
                }
            }

            return spStr;
        } catch (Exception e) {
            Log.e(TAG, "Exception:" + e.getMessage());
            return new SpannableStringBuilder(text);
        }

    }

    private ImageGetter mImageGetter = new ImageGetter() {

        @Override
        public Drawable getDrawable(String source) {
            Log.d(TAG, "ImageGetter.getDrawable()");
            Drawable drawable = null;

            if (!TextUtils.isEmpty(source)) {

                if (source.contains(ConstantsUtil.COMPAT_FACE_KEY)) {
                    drawable = FaceManager.getCompatFace(mContext, source);
                } else {
                    drawable = FaceManager.getFace(mContext, source);
                }
            }

            return drawable;
        }

    };

}
